//@version=5
indicator("Helacator Theta", overlay=true)  // Defines the indicator name and specifies that it should overlay on the chart

// Input parameters for MA lengths
ma1_length = input.int(50, title="MA 1 Length")  // User input for the first moving average length, default is 50
ma2_length = input.int(200, title="MA 2 Length")  // User input for the second moving average length, default is 200

// Input colors for labels
bullish_label_color = color.new(#e9812a, 0)  // Define the color for bullish signals (no transparency)
bearish_label_color = color.new(#de2463, 0)  // Define the color for bearish signals (no transparency)

// Inputs for Cooldown Filter
useCooldown = input(true, title="Use Cooldown Filter")  // User input to enable or disable the cooldown filter
cooldownPeriod = input.int(150, title="Cooldown Period (Candles)")  // User input for the cooldown period in candles, default is 150

// Moving averages based on input lengths
ma1 = ta.sma(close, ma1_length)  // Calculate the first simple moving average (SMA) based on user input length
ma2 = ta.sma(close, ma2_length)  // Calculate the second SMA based on user input length

// Function to detect Three White Soldiers pattern
three_white_soldiers() =>
    candle1 = close > open and close[1] > open[1] and close[2] > open[2]  // Condition for three consecutive bullish candles
    candle2 = open[1] <= close[2] and close[1] > close[2]  // Second candle opens within or near the body of the first and closes higher
    candle3 = open <= close[1] and close > close[1]  // Third candle opens within or near the body of the second and closes higher
    candle1 and candle2 and candle3  // Return true if all conditions are met, indicating a Three White Soldiers pattern

// Function to detect Three Black Crows pattern
three_black_crows() =>
    candle1 = close < open and close[1] < open[1] and close[2] < open[2]  // Condition for three consecutive bearish candles
    candle2 = open[1] >= close[2] and close[1] < close[2]  // Second candle opens within or near the body of the first and closes lower
    candle3 = open >= close[1] and close < close[1]  // Third candle opens within or near the body of the second and closes lower
    candle1 and candle2 and candle3  // Return true if all conditions are met, indicating a Three Black Crows pattern

// Variables to track the last signal
var bool last_was_buy = false  // Track if the last signal was a buy
var bool last_was_sell = false  // Track if the last signal was a sell

// Cooldown logic
var int lastBuySignal = na  // Store the bar index of the last buy signal
var int lastSellSignal = na  // Store the bar index of the last sell signal
cooldownBuyCondition = na(lastBuySignal) or (bar_index - lastBuySignal) > cooldownPeriod  // Condition to check if the cooldown period has passed since the last buy signal
cooldownSellCondition = na(lastSellSignal) or (bar_index - lastSellSignal) > cooldownPeriod  // Condition to check if the cooldown period has passed since the last sell signal

// Generate buy and sell signals with MA filters and cooldown
buy_signal = three_white_soldiers() and not last_was_buy and close > ma1 and close > ma2 and (not useCooldown or cooldownBuyCondition)  // Buy signal condition based on Three White Soldiers pattern, MAs, and cooldown
sell_signal = three_black_crows() and not last_was_sell and close < ma1 and close < ma2 and (not useCooldown or cooldownSellCondition)  // Sell signal condition based on Three Black Crows pattern, MAs, and cooldown

// Update last signal tracking and cooldown timers
if (buy_signal)
    last_was_buy := true  // Set last buy signal to true
    last_was_sell := false  // Reset last sell signal
    lastBuySignal := bar_index  // Record the current bar index for the buy signal

if (sell_signal)
    last_was_sell := true  // Set last sell signal to true
    last_was_buy := false  // Reset last buy signal
    lastSellSignal := bar_index  // Record the current bar index for the sell signal

// Plot buy and sell signals with specified colors
plotshape(series=buy_signal, location=location.belowbar, color=bullish_label_color, style=shape.labelup, text="BUY", textcolor=color.white, size=size.small)  // Plot buy signal
plotshape(series=sell_signal, location=location.abovebar, color=bearish_label_color, style=shape.labeldown, text="SELL", textcolor=color.white, size=size.small)  // Plot sell signal

// Change bar colors based on the last signal
var bool inBuy = false  // Track if currently in a buy condition
var bool inSell = false  // Track if currently in a sell condition

if buy_signal
    inBuy := true  // Set inBuy to true on buy signal
    inSell := false  // Reset inSell

if sell_signal
    inSell := true  // Set inSell to true on sell signal
    inBuy := false  // Reset inBuy

barcolor(inBuy ? bullish_label_color : na)  // Change bar color to bullish when in a buy condition
barcolor(inSell ? bearish_label_color : na)  // Change bar color to bearish when in a sell condition
